package cc.telecomdigital.tdstock.activity.groups.more;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;

/**
 * 財星Content
 * 
 * @author ouyangzhihui
 */
public class StockStarContentActivity extends HttpSuperClassStockBaseActivity implements OnClickListener
{
	final static private String	TAG			= "StockStarContentActivity";

	static public Class<?>		iFromClass	= null;
	static public String		iTypeName	= "";
	static public String		iContent	= "";
	static public int 	iPosition	 = 0;
	static private int[]		starArray	=
	{
		R.string.more_stockstar_01,
		R.string.more_stockstar_02,
		R.string.more_stockstar_03,
		R.string.more_stockstar_04,
		R.string.more_stockstar_05,
		R.string.more_stockstar_06
	};
	
	private Button				btnLast;
	private Button				btnNext;
	private Button				moreReturnBtn;
	private TextView			txtPageContent;
	private WebView			contentView;
	
	static public List<String> contentList = new ArrayList<String>();

	@Override
	protected ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_stockstar_content);

		moreReturnBtn = (Button) findViewById(R.id.more_returnbtn);
		btnLast = (Button) findViewById(R.id.btnLast);
		btnNext = (Button) findViewById(R.id.btnNext);
		
		moreReturnBtn.setOnClickListener(this);
		btnLast.setOnClickListener(this);
		btnNext.setOnClickListener(this);
		
		txtPageContent = (TextView) findViewById(R.id.txtPageContent);
//		contentView = (TextView) findViewById(R.id.content);
		contentView = (WebView) findViewById(R.id.webView);
		WebSettings webSettings = contentView.getSettings();
		webSettings.setDefaultFontSize(24);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		TDStockLog.d(TAG, "TagKe: " + iTypeName);
		
		int index = Integer.parseInt(iTypeName) - 1;
		moreReturnBtn.setText(starArray[index]);
		
		if(contentView == null)
			contentView = (WebView) findViewById(R.id.webview);
		
		contentView.clearCache(true);
		contentView.clearView();
		contentView.clearHistory();
		
		updateWebView();
		
//		contentView.setText(iContent);
		resetPageContent();
	}
	
	protected void onPause()
	{
		super.onPause();
		contentView.clearSslPreferences();
		contentView.loadUrl("about:blank");
		contentView.clearHistory();
	}

	private void updateWebView()
	{
		if(iContent == null)
			return;
		
		String summary = "<html><body><div>" + iContent.replaceAll("[\n]", "<br>").replaceAll("&gt;", ">").replaceAll("&lt;", "<").replaceAll("&amp;", "&") + "</div></body></html>";
		
		contentView.getSettings().setJavaScriptEnabled(true);
		contentView.getSettings().setBuiltInZoomControls(true);
		contentView.loadDataWithBaseURL(null, summary, "text/html", "utf-8", null);
	}
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.more_returnbtn:
			{
				if(iFromClass != null && StockStarListActivity.class == iFromClass)
					StockStarListActivity.iContentPageBack = true;
				else if(iFromClass != null && StockStarIndexActivity.class == iFromClass)
					StockStarIndexActivity.iContentPageBack = true;
				SwitchBackActivity();
			}
				break;
			case R.id.btnLast:
				last();
				break;
			case R.id.btnNext:
				next();
				break;
		}
	}
	
	private void last()
	{
		if(contentList.size() <= 0)
			return;
		
		iPosition--;
		iContent = contentList.get(iPosition-1);
		updateWebView();
//		contentView.setText(contentList.get(iPosition-1));
		resetPageContent();
	}
	
	private void next()
	{
		if(contentList.size() <= 0)
			return;
		
		if(btnNext.getText().toString().equals(getResources().getString(R.string.more_stockstar_backmenu)))
		{
			if(iFromClass != null && StockStarListActivity.class == iFromClass)
				StockStarListActivity.iContentPageBack = true;
			else
				StockStarListActivity.iTypeName = iTypeName;
			SwitchForwardActivity(StockStarListActivity.class);
			return;
		}
		
		iPosition++;
		iContent = contentList.get(iPosition-1);
		updateWebView();
//		contentView.setText(contentList.get(iPosition-1));
		resetPageContent();
	}
	
	private void resetPageContent()
	{
		if(contentList.size() > 0)
		{
			txtPageContent.setVisibility(View.VISIBLE);
			txtPageContent.setText(iPosition + "/" + contentList.size());
		}
		else
		{
			txtPageContent.setVisibility(View.INVISIBLE);
		}
		
		if(iPosition <= 1)
		{
			btnLast.setEnabled(false);
//			btnLast.setVisibility(View.INVISIBLE);
		}
		else
		{
			btnLast.setEnabled(true);
			btnLast.setVisibility(View.VISIBLE);
		}
		
		if(iPosition >= contentList.size())
		{
			if(contentList.size() <= 0)
				btnNext.setEnabled(false);
//				btnNext.setVisibility(View.INVISIBLE);
			else
				btnNext.setText(getResources().getString(R.string.more_stockstar_backmenu));
		}
		else
		{
			btnNext.setEnabled(true);
//			btnNext.setVisibility(View.VISIBLE);
			btnNext.setText(getResources().getString(R.string.more_stockstar_next));
		}
		
		if(contentList.size() == 0)
		{
			btnLast.setVisibility(View.INVISIBLE);
			btnNext.setVisibility(View.INVISIBLE);
		}
		else
		{
			btnLast.setVisibility(View.VISIBLE);
			btnNext.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		if(iFromClass != null && StockStarListActivity.class == iFromClass)
		{
			if(contentList.size() > 0)
				contentList.remove(contentList.size()-1);
			return null;
		}
		
		String[] iTime = HttpAppInfo.GetDataTimeParamsByYear(null);
		
		TDStockLog.d(TAG, "Time:" + iTime[0] + "," + iTime[1]);
		
		contentList.clear();
		
		return HttpAppInfo.SendStockStarRequest(actContext, iTypeName, iTime[0], iTime[1], this);
	}

	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(H_UserClient_HttpRequest_Result iResult)
	{
		if (!iResult.MStatus.equals("0"))
		{
			ShowMessageDialog(iResult.MError + "(" + iResult.MStatus + ")");
			return;
		}
		
		for(String tmp : iResult.contentStores.get(0))
		{
			try
			{
				if(tmp == null)
					contentList.add("---");
				else
					contentList.add(tmp);
			}
			catch(Exception e)
			{
				contentList.add("---");
			}
		}
		
		resetPageContent();
	}
	
}
